﻿#include "stdafx.h"
#include "CTriangleEX.h"
using namespace std;

float sign(POINT p1, POINT p2, POINT p3)
{
  return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y);
}

/** 
 * CRectangle includes follwing functions: 
	1. Constructor

	2. SetInfor (full args)
	3. Set Color: pen or brush color
	4. Set width or pattern
	5. Set style
	6. Set filled or not filled
	7. Set cordinates

	8. GetInfor (full args)
	9. Get Color: pen or brush color
	10. Get width or pattern
	11. Get style
	12. Get filled or not filled
	13. Get cordinates. 
	
	14. Move
	15. View
	16. IsInShape(POINT) //do nothing.  Click chon
	17. IsInsShape (POINT, POINT) . Quet chon
	18. IsResizing. return vi tri dau mút de resize
	19. Resize. Thuc hien resize lai kich thuoc shape
 */

/* 1. Constructor */
CTriangleEx::CTriangleEx()
{}

/* 2. SetInfor (full arg)*/
void CTriangleEx::SetInfor(int _left, int _top, int _right, int _bottom, bool _filled,
							int _penWidth, int _penStyle, COLORREF _brushColor, COLORREF _penColor,
							int _pattern) 
{
	CShapeEx::SetInfor(_left, _top, _right, _bottom, _filled,
						_penWidth, _penStyle, _brushColor, _penColor,
						_pattern);
}

/* 3. SetInfor color */
void CTriangleEx::SetInfor(int _type, COLORREF _color)
{
	CShapeEx::SetInfor(_type, _color);
}

/* 4. SetInfor width or pattern */
void CTriangleEx::SetInfor(int _type, int _value)
{
	CShapeEx::SetInfor(_type, _value);
}

/* 6. Set filled or not filled */
void CTriangleEx::SetInfor(bool filled)
{
	CShapeEx::SetInfor(filled);
}

/* 7. SetInfor cordinate */
void CTriangleEx::SetInfor(int _left, int _top, int _right, int _bottom)
{
	CShapeEx::SetInfor(_left, _top, _right, _bottom);
}

/* 8. GetInfor (full arguments)*/
void CTriangleEx::GetInfor(int& _left, int& _top, int& _right, int& _bottom, bool& _filled,
							int& _penWidth, int& _penStyle, COLORREF& _brushColor, COLORREF& _penColor,
							int& _pattern)
{
	CShapeEx::GetInfor(_left, _top, _right, _bottom, _filled,
						_penWidth, _penStyle, _brushColor, _penColor,
						_pattern);
}

/* 9. Get color */
void CTriangleEx::GetInfor(int _type, COLORREF& _color)
{
	CShapeEx::GetInfor(_type, _color);
}

/* 10. Get width or pattern */
void CTriangleEx::GetInfor(int _type, int& _value)
{
	CShapeEx::GetInfor(_type, _value);
}

/* 11. Get Style */
void CTriangleEx::GetInfor(int& _style)
{
	CShapeEx::GetInfor(_style);
}

/* 12. Get filll or not filled */
void CTriangleEx::GetInfor(bool& _filled)
{
	CShapeEx::GetInfor(_filled);
}

/* 13. Get cordinate */
void CTriangleEx::GetInfor(int& _left, int& _top, int& _right, int& _bottom)
{
	CShapeEx::GetInfor(_left, _top, _right, _bottom);
}

/* 14. Move */ 
void CTriangleEx::Move(int dx, int dy) 
{
	CShapeEx::Move(dx, dy);
}

/* 15. View */
void CTriangleEx::View(HDC hdc)
{
	int _left;
	int _top;
	int _right;
	int _bottom;
	bool _filled;
	int _penWidth;
	int _penStyle;
	COLORREF _brushColor;
	COLORREF _penColor;
	int _pattern;
	HPEN hPen, hOldPen;
	HBRUSH hBrush, hOldBrush;
	POINT point[3];
	
	this->GetInfor(_left, _top, _right, _bottom, _filled,
					_penWidth, _penStyle, _brushColor, _penColor, _pattern);

	if (_filled)
	{
		point[0].x = (_left + _right) / 2;
		point[0].y = _top;
	
		point[1].x = _left;
		point[1].y = _bottom;

		point[2].x = _right;
		point[2].y = _bottom;

		hBrush = CreateSolidBrush(_brushColor);
		hOldBrush = (HBRUSH)SelectObject(hdc, hBrush);
		Polygon(hdc, point, 3);
		SelectObject(hdc, hOldBrush);
		DeleteObject(hBrush);
	}

	hPen = CreatePen(_penStyle, _penWidth, _penColor);
	hOldPen = (HPEN)SelectObject(hdc, hPen);

	/**/
	MoveToEx(hdc, (_left + _right) / 2, _top, NULL);
	LineTo(hdc, _left, _bottom);

	/**/
	MoveToEx(hdc, _left, _bottom, NULL);
	LineTo(hdc, _right, _bottom);

	/**/
	MoveToEx(hdc, _right, _bottom, NULL);
	LineTo(hdc, (_left + _right) / 2, _top);

	

	SelectObject(hdc, hOldPen);
	DeleteObject(hPen);	
}

/* 16. IsInShape */
bool CTriangleEx::IsInShape(POINT p)
{
	int _left;
	int _top;
	int _right;
	int _bottom;
	bool b1;
	bool b2;
	bool b3;
	POINT v1;
	POINT v2;
	POINT v3;
	
	GetInfor(_left, _top, _right, _bottom);

	/**/
	v1.x = (_left + _right) / 2;
	v1.y = _top;

	/**/
	v2.x = _right;
	v2.y = _bottom;

	/**/
	v3.x = _left;
	v3.y = _bottom;

	/**/
	b1 = sign(p, v1, v2) < 0.0f;
	b2 = sign(p, v2, v3) < 0.0f;
	b3 = sign(p, v3, v1) < 0.0f;
	
	return ((b1 == b2) && (b2 == b3));	
}

/* 17. IsInShape (2 args) */
bool CTriangleEx::IsInShape(POINT point1, POINT point2)
{
	return CShapeEx::IsInShape(point1, point2);
}

/* 18. IsResizing */
int CTriangleEx::isResizing(POINT p)
{
	int left;
	int right;
	int bottom;
	int top;

	GetInfor(left, top, right, bottom);

	if (p.x >= (left + right)/2 - 5 && p.x <= (left + right)/2 + 5 &&
		p.y >= top - 5 && p.y <= top + 5)
		 return _MIDDLELEFTRIGHTTOP_;

	if (p.x >= left- 5 && p.x <= left + 5 &&
		p.y >= bottom - 5 && p.y <= bottom + 5)
		 return _LEFTBOTTOM_;

	if (p.x >= right- 5 && p.x <= right + 5 &&
		p.y >= bottom - 5 && p.y <= bottom + 5)
		 return _RIGHTBOTTOM_;

	return -1;
}

/* 19. Resize() */
void CTriangleEx::Resize(int dx, int dy, int position)
{
	CShapeEx::Resize(dx, dy, position);
}
